const PENDING = 'pending'
const ACTIVE = 'active'
const OPTIONS = {
    timer: 60
}

class tiemrs {
    constructor(backCall = () => { }, options) {
        this.state = PENDING
        this.backCall = backCall
        this.options = options || OPTIONS
        this.timer = this.options.timer
    }

    begin() {
        if (this.state === PENDING && this.timer > 0) {
            this.state = ACTIVE
            this.beginTimer()
        }
    }

    beginTimer() {
        this.backCall(this.timer, this.state)
        if (this.state === ACTIVE) {
            setTimeout(() => {
                this.timer--
                if (this.timer <= 0) {
                    this.pause()
                }
                this.beginTimer()
            }, 1000)

        }
    }

    pause() {
        this.state = PENDING
    }

    reset() {
        this.state = PENDING
        this.timer = this.options.timer
        this.beginTimer()
    }

}

export default tiemrs
